var fs = require("fs");
var path = require("path");

var resource_path = path.join(__dirname,'..','GameHall','resource');


function read_json(_path)
{
	// 同步读取
	var data = fs.readFileSync(_path);
	return JSON.parse(data.toString());
}

function write_json(data,_path)
{
	fs.writeFileSync(_path,JSON.stringify(data));
}

function reWriteDefault(_type,groups,new_groups,index)
{
	
	let new_key = [];
	
	let pub = 'pub';
	let type = _type;

	console.log(groups.length);

	for (var i = groups.length - 1; i >= 0; i--)
	{
		let keys_str = groups[i].keys;
		let name_str = groups[i].name;
		let arr = keys_str.split(',');
		//小于等于3则视为需要被合并的项
		if(arr.length <= 3 && name_str.indexOf(type) > -1)
		{
			new_key = new_key.concat(arr);
			//删除需要合并的项
			groups.splice(i,1);
			// i = groups.length - 1;
		}
		else
		{
			continue;
		}

		if(new_key.length >= 5)
		{
			let keys = new_key.join(',');
			let name = pub + index;
			new_groups.push({keys:keys,name:name});
			index++;
			new_key = [];
			console.log('index:',index);
		}
	}
	console.log(new_groups.length);
	console.log(groups.length);
	return index;
}


function init()
{
	let default_path = path.join(resource_path,'default.res.json');
	let default_obj = read_json(default_path);
	let groups = default_obj.groups;
	let new_groups = [];
	let index = 0;//自增的项

	index = reWriteDefault('m_pub',groups,new_groups,index);
	reWriteDefault('pc_pub',groups,new_groups,index);

	write_json(new_groups,'new_groups');
	write_json(groups,'groups');
}

init();
